package com.smedi.ismedi.progress.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.authority.starter.util.AuthorityUtil;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectBasicResponse;
import com.smedi.ismedi.progress.core.application.command.VersionManageListQuery;
import com.smedi.ismedi.progress.core.domain.aggregatemodel.VersionManage;
import com.smedi.ismedi.progress.core.domain.repository.FileRepository;
import com.smedi.ismedi.progress.core.domain.repository.ProgressProjectRepository;
import com.smedi.ismedi.progress.core.domain.repository.VersionManageRepository;
import com.smedi.ismedi.progress.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.progress.core.port.adapter.persistence.model.VersionManageEntity;
import com.smedi.ismedi.progress.core.port.adapter.persistence.mybatis.mapper.VersionManageMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 基础信息仓库实现类
 *
 * @author cookie
 * @date 2022/07/14
 */
@Repository
@AllArgsConstructor
public class VersionManageRepositoryImpl implements VersionManageRepository {
    private final AdapterConverter adapterConverter;
    private final FileRepository fileRepository;
    private final AuthorityUtil authorityUtil;
    private final VersionManageMapper versionManageMapper;
    private final ProgressProjectRepository progressProjectRepository;

    @Override
    public Page<VersionManage> getVersionList(VersionManageListQuery versionManageListQuery) {
        ProjectBasicResponse project = progressProjectRepository.getProject(versionManageListQuery.getProjectId());
        Page<VersionManageEntity> page = new Page<>(versionManageListQuery.getOffset(),versionManageListQuery.getPageSize());
        LambdaQueryWrapper<VersionManageEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtils.isNotNull(versionManageListQuery.getProjectId()),VersionManageEntity::getProjectId,versionManageListQuery.getProjectId());
        lambdaQueryWrapper.orderByDesc(VersionManageEntity::getVersion);
        Page<VersionManageEntity> versionManageEntityPage = versionManageMapper.selectPage(page, lambdaQueryWrapper);
        Page<VersionManage> versionManages = adapterConverter.versionManageEntityToVersionManage(versionManageEntityPage);
        versionManages.getRecords().forEach(v->{
            v.setMainCourtyardId(project.getHrOrganizationId());
        });
        return versionManages;
    }

}